package com.ferryc.tcc.impl;

import com.ferryc.entity.Order;
import com.ferryc.exception.BaseException;
import com.ferryc.mapper.IOrderMapper;
import com.ferryc.mapper.OrderDetailMapper;
import com.ferryc.model.Product;
import com.ferryc.service.IProductsService;
import com.ferryc.tcc.IOrderTccService;
import com.ferryc.tccfegin.PayTccFeignClient;
import com.ferryc.tccfegin.StroageTccFeignClient;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;

/**
 * Created by Ferry Chen on 2019/11/26.
 */
@Slf4j
@Service("orderTccServiceCancel")
public class OrderTccServiceCancel implements IOrderTccService {

    @Autowired
    private OrderDetailMapper orderDetailMapper;

    @Autowired
    private IOrderMapper orderMapper;

    @Autowired
    private StroageTccFeignClient stroageTccFeignClient;
    @Autowired
    private PayTccFeignClient payTccFeignClient;

    @Transactional(noRollbackFor = BaseException.class)
    @Override
    public int saveOrder(String orderId,String userId, List<Product> productsList) {
        log.info("cancel save.....");
        Order order=orderMapper.selectById(orderId);
        for (Product product : productsList) {
            stroageTccFeignClient.consume(product.getProductId(), product.getCount());
        }
        payTccFeignClient.consume(userId,order.getMoney());
        Order o = new Order();
        o.setId(orderId);
        o.setStatus("3");
        o.setUpdateTime(new Date());
        orderMapper.updateOrder(o);
        return 1;
    }
}
